<?php
/**
 * Created by PhpStorm.
 * User: lz
 * Date: 2017/8/30
 * Time: 11:04
 */

namespace app\modules\admin\controllers;


use app\models\yuyue\Yuyue;
use app\modules\admin\common\BaseController;
use app\common\service\UtilService;
use app\common\service\UrlService;

class YuyueController extends BaseController
{
    public function actionIndex()
    {
        $status = $this->get("status", 0);
        $mix_kw = $this->get("mix_kw");

        $p = intval($this->get("p", 1));

        $query = Yuyue::find();

        if ($status > -1) {
            $query->andWhere(["status" => $status]);
        }

        if ($mix_kw) {
            $where_nickname = ['LIKE', 'member_nickname', '%' . $mix_kw . '%', false];
            $where_mobile = ['LIKE', 'phone', '%' . $mix_kw . '%', false];
            $query->andWhere(['or', $where_nickname, $where_mobile]);
        }


        $total_res_count = $query->count(); //共有多少条数据
        $pages = UtilService::ipagination([
            'total_count' => $total_res_count,
            'page_size' => $this->page_size,
            'page' => $p,
            'display' => 10
        ]);

        $list = $query->orderBy(["creat_time" => SORT_DESC])
            ->offset(($p - 1) * $this->page_size)
            ->limit($this->page_size)->all();

        return $this->render('index',
            [
                "list" => $list,

                "search_conditions" => [
                    "mix_kw" => $mix_kw,
                    "status" => $status
                ],

                "pages" => $pages

            ]);
    }

    //操作方法
    public function actionOps()
    {
        $id = $this->post('id', 0);
        $act = trim($this->post('act', ''));

        if (!$id) {
            return $this->renderJSON([], "请选择要操作的账号~~", -1);
        }

        if (!in_array($act, ['remove', 'recover'])) {
            return $this->renderJSON([], "操作有误，请重试~~", -1);
        }

        $info = Yuyue::find()->where(['id' => $id])->one();
        if (!$info) {
            return $this->renderJSON([], "指定账号不存在~~", -1);
        }

        if ($act == "recover") {
            $info->status = 1;
            $info->update(0);
            return $this->renderJSON([], "操作成功~~");
        }

        if ($act == "remove") {
            $info->delete();
            return $this->renderJSON([], "操作成功~~");
        }

    }

    public function actionInfo()
    {
        $id = $this->get("id", "");
        $reback_url = UrlService::buildAdminUrl("/account/index");

        if (!$id) {
            return $this->redirect($reback_url);
        }

        $yuyue_info = Yuyue::find()->where(['id' => $id])->one();

        return $this->render("info", [
            'info' => $yuyue_info
        ]);
    }

    public function actionSet()
    {
        if (\Yii::$app->request->isGet) {
            $id = intval($this->get("id", 0));
            $info = [];
            if ($id) {
                $info = Yuyue::find()->where(['id' => $id])->one();
            }
            return $this->render("set", [
                'info' => $info
            ]);
        }

        $id = intval($this->post("id", 0));
        $member_nickname = trim($this->post("member_nickname", ""));
        $phone = $this->post("phone", "");
        $time = $this->post("time", "");
        $name = trim($this->post("name", ""));
        $remark = $this->post("remark", "");

        $info = Yuyue::find()->where(['id' => $id])->one();

        $info->member_nickname = $member_nickname;
        $info->phone = $phone;
        $info->time = $time;
        $info->name = $name;
        $info->remark = $remark;

        $info->update(0);

        return $this->renderJSON([], "操作成功~~");
    }
}